// pages/collection/collection.js
var app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    // 收藏商品信息
    collection: [],
    notshowEmpty: false
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    app.getList("collection")
      .then(res => {
        console.log('请求成功', res.data)
        if (res.data.length != 0) {
          this.setData({
            collection: res.data
          })
        } else {
          this.setData({
            notshowEmpty: true,
            collection: res.data
          })
        }
      })
      .catch(err => {
        console.log('请求失败', err)
      })
  },
  delCollection(e) {
    console.log("特产id为" + e.currentTarget.dataset.id)
    app.removeOne('collection', e.currentTarget.dataset.id)
      .then(res => {
        this.data.collection.splice(e.currentTarget.dataset.index, 1)
        this.setData({
          collection: this.data.collection
        })
        console.log('删除成功', res)
        this.lastOne()
      })
      .catch(err => {
        console.log('删除失败', err)
      })
  },
  lastOne() {
    app.getList("collection")
      .then(res => {
        if (res.data.length === 0) {
          this.setData({
            notshowEmpty: false,
          })
        }
      })
      .catch(err => {
        console.log('查询失败', err)
      })
  },
  goToDetail(e){
    wx.navigateTo({
      url: '../productDetail/productDetail?id='+e.currentTarget.dataset.id,
    })
  }
})